//
//  ReweetedStatusView.swift
//  Weibo
//
//  Created by 周先生 on 15/11/19.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit
import SnapKit
class ReweetedStatusView: UIView {


    /**
     转发微博底部约束 用来记录底部约束 后续给据是否有配图 来进行动态修改
     */
    var bottomConstraints: Constraint?

    //数据模型
    var reweetedStatus: Status? {
    
        didSet {
            reweetedLabel.text = reweetedStatus?.text
            
            //移除底部约束
            self.bottomConstraints?.uninstall()
            if let picurls = reweetedStatus?.imageUrls where picurls.count > 0 {
                //设置数据源
                pictureView.imageUrls = picurls
                //显示配图视图
                pictureView.hidden = false
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            } else {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成约束类型
                    self.bottomConstraints = make.bottom.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            
            }
        
        
        }
    
    }
    
    //3,重写父类构造方法 调用设置UI方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //2,添加子控件 设置约束
    private func setupUI() {
    
        addSubview(reweetedLabel)
        addSubview(pictureView)
        //设置约束
        reweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(reweetedLabel.snp_left)
//            make.width.equalTo(100)
//            
//            make.height.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    
    }
    //1,懒加载子控件
    private lazy var reweetedLabel: UILabel = UILabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    //配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
